package com.hex.liyue.lookingdatemodule.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.adapter.PagerAdapter;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.lookingdatemodule.fragment.FragmentHotNew;
import com.hex.liyue.lookingdatemodule.view.PopAgeSift;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 这是寻找约会界面
 * Created by WJ on 2016/6/8.
 */
public class HotNewDateActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {
    @Bind(R.id.vp_hot_new)
    ViewPager vpHotNew;
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_right)
    TextView tvToobarRight;

    @Bind(R.id.rg_looking)
    RadioGroup rgLooking;
    @Bind(R.id.rb_hot)
    RadioButton rbHot;
    @Bind(R.id.rb_new)
    RadioButton rbNew;
    @Bind(R.id.v_pop)
    View vPop;


    private PopAgeSift popAgeSift;
    private List<Fragment> list = new ArrayList<>();
    private FragmentHotNew fragmentHot = FragmentHotNew.newInstance("hot");
    private FragmentHotNew fragmentNew = FragmentHotNew.newInstance("new");
    private int position = 0;//0热门 1最新

    @Override
    protected void bindPresenter() {

    }

    @Override
    protected void initData(Bundle extras) {
        list.add(fragmentHot);
        list.add(fragmentNew);
        PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), list);
        vpHotNew.setAdapter(pagerAdapter);
        vpHotNew.setOnPageChangeListener(this);
        rgLooking.setOnCheckedChangeListener(this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_looking_date;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToobarBack.setVisibility(View.VISIBLE);
        rgLooking.setVisibility(View.VISIBLE);
        tvToobarRight.setVisibility(View.VISIBLE);
        tvToobarRight.setText("筛选");
    }


    @OnClick({R.id.iv_toobar_back, R.id.tv_toobar_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
            case R.id.tv_toobar_right:
                showPopAgeSift();
                break;
        }
    }

    /**
     * 显示性别筛选框
     */
    private void showPopAgeSift() {
        if (popAgeSift == null) {
            popAgeSift = new PopAgeSift(this);
            popAgeSift.setOnBtnClickListener(new PopAgeSift.OnBtnClickListener() {
                @Override
                public void onBtnWoManClickListener() {
                    if (position == 0) {
                        fragmentHot.setSex(2);
                    } else {
                        fragmentNew.setSex(2);
                    }
                }

                @Override
                public void onBtnManClickListener() {
                    if (position == 0) {
                        fragmentHot.setSex(1);
                    } else {
                        fragmentNew.setSex(1);
                    }
                }

                @Override
                public void onBtnAllClickListener() {
                    if (position == 0) {
                        fragmentHot.setSex(3);
                    } else {
                        fragmentNew.setSex(3);
                    }
                }

            });
            popAgeSift.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    lighton();
                }
            });
        }
        if (!popAgeSift.isShowing()) {
            popAgeSift.showAtLocation(vPop, Gravity.BOTTOM, 0, 0);
            lightof();
        }
    }

    private void lighton() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 1.0f;//透明度
        getWindow().setAttributes(lp);
    }

    private void lightof() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.3f;//透明度
        getWindow().setAttributes(lp);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.rb_hot) {
            vpHotNew.setCurrentItem(0);
        } else if (checkedId == R.id.rb_new) {
            vpHotNew.setCurrentItem(1);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        this.position = position;
        if (position == 0) {
            rbHot.setChecked(true);
        } else if (position == 1) {
            rbNew.setChecked(true);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

}
